package com.hange.ymcc;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.junit.Test;

public class Alipaytest {
    @Test
    public void test1() {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
//            AlipayTradePrecreateResponse response = Payment.FaceToFace()
//                    .preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
//pay(String subject, String outTradeNo, String totalAmount, String returnUrl)
            // returnUrl：  页面成功的地址
            AlipayTradePagePayResponse alipayTradePagePayResponse = Factory.Payment.Page().pay(
                    "测试使用",
                    "2234567890",
                    "5799.00",
                    "http://www.baidu.com");

            System.out.println(alipayTradePagePayResponse);
            // 3. 处理响应或异常
            if (ResponseChecker.success(alipayTradePagePayResponse)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + alipayTradePagePayResponse.getBody() + "，" + alipayTradePagePayResponse.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";
        config.signType = "RSA2";  // 签名类型
        config.appId = "9021000149696370";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGA+mdbG9W+NFfKl1IOnxua5y5ptJ6C5/Sia68NZqWnTG86UprCQuOAYKHcwf+i1B+sMf6Qu+a7f4GizypFJqMUG5PPvsGRJcUGKh7uu9u0UwaSh3EofXiNbdwJ/1sVN5RBOVH67LXQSBB5AFkvNeBE2MMkS+HkwKZS252xLnIdBlf3BHNEVR0NdvZsiVG/T43+68FQp9sKTB5Gvf2nE33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jaGJnJbcumfzQA/DDDCDgfxk+12lO/fBriWWCYLH8Y0yC3KO3WqmYP+jjAXb+4OxRUHJjQAdAaOEh5cawnbUmwwQT/7BwA3d5AoGACa6Uy/WCe4Gp89F3PpHkLoR+mKui81lm+sBuerFRB/x251inMm37wQE7pkMXVuZYRz1mDluCp6iTtPs6fQl9FU+JI26w2M/ffjvXcP/CR+Y4IFQ/9Ph19kJcwRRUQEqcDfDF0D6xvugoMi3s0/eUd8gAK+eLNZHLobRB3su3+pE=";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlnOW5aUibxJwHWO6rtqhgz4VcNKZr5+Fn+2PhS8QZHFrnkpH9T3hNFJnIM3STYu9ss2fooxQX2EtyeYEJ75CptcgcVtM8xIT1t2rk1GmGVSlx3A/hNRu9U4u3WSm30FXrd2VEnhv5g6J6QQREYmQM7x+o6yvXckMmLRthwD1ExAeF7HCTBnyQ26NhMdbdUZujQx5ooJ1J9LIdpJ7rwUyIAExK99QEb3DtBM/SV12ZOF4zUeFlc4z4j+xZMPQW98lgJ5u3qszNRqbu9gbDGG5w2fE7d27i2tXI9UFpdUADBRKhwMJ16Hqxn/tTnNKwoTlbuI56jhMhx9gwa4tjhb7owIDAQAB";
        //可设置异步通知接收服务地址
        config.notifyUrl = "http://localhost:10010/pay/notifyUrl";
        return config;
    }
}
